package com.example.server;


import javafx.scene.control.TextArea;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class MyServer implements Runnable{

    //Server端监听的端口号
    public static final int PORT = 8888;

    TextArea message;

    public MyServer(){
    }

    public MyServer(TextArea message){
        super();
        this.message = message;
    }

    @Override
    public void run(){
        if(Thread.currentThread().isInterrupted())
            return;
        ServerSocket serverSocket;
        Socket socket;
        try{
            serverSocket = new ServerSocket(PORT);//捕捉异常
            while(true){
                System.out.println("MyServer.run" + "----启动");
                socket = serverSocket.accept();
                System.out.println("MyServer.run" + "----接收到客户端连接");
                //一个客户端介入就启动一个handler线程去处理
                new Thread(new Handler(message,socket)).start();
            }
        }catch(IOException e){
            e.printStackTrace();
        }
    }
}